<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property string $click_id 
 * @property int $click_time 
 * @property string $account_id
 * @property string $device_os_type 
 * @property string $hash_android_id 
 * @property string $muid 
 * @property string $hash_oaid 
 * @property string $promoted_object_id 
 * @property string $campaign_id
 * @property string $adgroup_id
 * @property string $ad_id
 * @property string $ip 
 * @property string $user_agent 
 * @property string $promoted_object_type 
 * @property string $callback 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 */
class OcpcTencent extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'ocpc_tencent';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    protected $guarded = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [ 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    /**
     * 根据oaid查询是否存在
     * @param string $oaid
     * @return bool
     */
    public static function existsOaid(string $oaid)
    {
        if (empty($oaid)) {
            return false;
        }

        return self::where('hash_oaid', $oaid)
//            ->where('channel_id', $channel)
            ->exists();
    }

    /**
     * 根据imei查询是否存在
     * @param string $muid
     * @return bool
     */
    public static function existMuid(string $muid)
    {
        if (empty($imei)) {
            return false;
        }

        return self::where([
            'muid'    => $muid,
//            'channel_id' => $channel
        ])->exists();
    }

    /**
     * @param array $params
     * @return int
     */
    static function addLog(array $params = []): int
    {
//        $oaid = $params['oaid'] ?? '';

        $data = array_merge($params,[
            'created_at'  => date('Y-m-d H:i:s'),
            'updated_at'  => date('Y-m-d H:i:s')
        ]);

        return self::insertGetId($data);
    }


    /**
     * 处理查询
     * @param $info
     * @return array
     */
    public static function handleSearch($info): array
    {
        //oaid 查询
        if (isset($info['oaid']) && $info['oaid']) {
            $res = self::where('hash_oaid', strtolower(md5($info['oaid'])))
//                ->where("channel_id", $info['channel'])
                ->orderBy('created_at', "desc")->first(['id', 'muid','hash_oaid','callback']);

            if ($res) {
                return $res->toArray();
            }
        }

        if (isset($info['imei']) && $info['imei']) {
            $res = self::where('muid', strtolower(md5($info['imei'])))
//                ->where("channel_id", $info['channel'])
                ->orderBy('created_at', "desc")->first(['id', 'muid','hash_oaid','callback']);

            if ($res) {
                return $res->toArray();
            }
        }

        return [];
    }
}